package com.uroad.liaoningpos.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;

/**
 * @创建者 wlw
 * @创建时间 2019/4/8 10:49
 * @描述
 */
public class BitmapUtil {

    /**
     * View转bitmap并保存在本地
     * @param view
     * @param path 路径
     * @param fileName 文件名
     * @param isAllView view在当前页面是否已全部显示
     * @return
     */
    public static String viewSaveToImage(View view, File path, String fileName,boolean isAllView) {
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        view.setDrawingCacheBackgroundColor(Color.WHITE);
        Bitmap cachebmp = null;
        // 把一个View转换成图片
        if (isAllView) {
            cachebmp = createBitmap(view);
        }else {
            cachebmp = loadBitmapFromView(view);
        }

        FileOutputStream fos=null;
        try {
            // 判断手机设备是否有SD卡
            boolean isHasSDCard = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
            if (isHasSDCard) {
                // SD卡根目录
//                File sdRoot = Environment.getExternalStorageDirectory();
                File file = new File(path, fileName);
                fos = new FileOutputStream(file);
            } else
                throw new Exception("创建文件失败!");

            cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);

            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        view.destroyDrawingCache();
        return path.getAbsolutePath() + "/" + fileName;
    }
    /**
     * view转bitmap 适用于整个View在当前页面已全部显示了
     * @param view
     * @return
     */
    public static Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    /**
     * view转bitmap 适用于当前页面没有显示或显示不完全的情况
     * @param view
     * @return
     */
    public static Bitmap loadBitmapFromView(View view) {
        int w = view.getWidth();
        int h = view.getHeight();

        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        // 加入到弱引用
        WeakReference<Bitmap> mReference1 = new WeakReference<Bitmap>(bmp);
        // 释放硬引用
        bmp = null;
        // 获取软引用中的位图变量
        bmp =  mReference1.get();
        Canvas canvas = new Canvas(bmp);
        view.draw(canvas);

        int width = w*h/380;
        Bitmap bm = Bitmap.createScaledBitmap(bmp, width, h, true);

        // 加入到弱引用
        WeakReference<Bitmap> mReference2 = new WeakReference<Bitmap>(bm);
        // 释放硬引用
        bm = null;
        // 获取软引用中的位图变量
        bm =  mReference2.get();
        Canvas c = new Canvas(bmp);


        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色，则生成透明 */

        view.layout(0, 0, w, h);
        view.draw(c);

        return bmp;
    }

}
